<?php
class cardController extends appController
{
    public function index(){
        $page = yf_empty($_REQUEST['page'], 1, true);
        $description = $_REQUEST['description'];
        $id = $_REQUEST['id'];
        $list = Model('card')->get_index($description,$id,$page);
        foreach($list['data'] as $k => $v)
        {
            
            $list['data'][$k]['dotime2'] = date('Y-m-d ',$v['dotime2']);
        }
        $this->assign('result',$list['data']);
        $this->assign('user_list_page',$list['pagelink']);
        $this->display('index.html');
    }
    
    //增加卡券
    public function add_index()
    {   
        $this->display('add_index.html');
    }
    
   //保存新卡券信息
   public function set_index()
   {
       $message = array('error'=>1,'content'=>'');
       $id = $_POST['id'];
       $card_id = $_POST['card_id'];
       $name = $_POST['name'];
       $description = $_POST['description'];
       $status = isset($_POST['status'])?intval($_POST['status']):0;
       $is_chat = isset($_POST['is_chat'])?intval($_POST['is_chat']):0;
       $day_max = isset($_POST['day_max'])?intval($_POST['day_max']):1;
       $hour_max = isset($_POST['hour_max'])?intval($_POST['hour_max']):1;
       $min_max = isset($_POST['min_max'])?intval($_POST['min_max']):1;
       $img = $_POST['img'];
//        $get_total = $_POST['get_total'];
//        if($get_total < 0)
//        {
//            $message['content'] = '已领数错误';
//            die(json_encode($message));
//        }
       if($day_max < 1)
       {
           $message['content'] = '每日限领量错误';
           die(json_encode($message));
       }
       if($hour_max < 1)
       {
           $message['content'] = '每小时限领量错误';
           die(json_encode($message));
       }
       if($min_max < 1)
       {
           $message['content'] = '每分钟限领量错误';
           die(json_encode($message));
       }
       if($day_max < $hour_max)
       {
           $message['content'] = '每日限领量不能小于每小时限领量';
           die(json_encode($message));
       }
       if($hour_max < $min_max)
       {
           $message['content'] = '每小时限领量不能小于每分钟限领量';
           die(json_encode($message));
       }
       
       $change_integral = $_POST['change_integral'];       
       
       $dotime2 = strtotime($_POST['dotime2']);
      
       $result = Model('card')->set_index($card_id,$hour_max,$min_max,$id,$name,$img,$status,$description,$change_integral,$dotime2,$is_chat,$day_max);
       if($result)
       {
           $message['error'] = 0;
           $message['content'] = '保存成功';
           die(json_encode($message));
       }else{
           $message['content'] = '操作失败，请重试！';
           die(json_encode($message));
       }
   }
    
   //修改卡券信息
   public function update_index()
   {
       $id = $_GET['id'];
       $res = Model('card')->get_index_one($id);
       $this->assign("res",$res);
       $this->display('add_index.html');
   }
    
   //删除指定记录
   public function del_index()
   {
       $id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
        
       if(is_array($id))
       {
           foreach($id as $k=>$v)
           {
               $result=Model('card')->del_index($v);
           }
       }
       else
       {
           $id=intval($id);
            
           $result=Model('card')->del_index($id);
       }
       print_r(json_encode($result));
   }
 
   //统计页面
   public function tongji()
   {
       $page = yf_empty($_REQUEST['page'], 1, true);        
       $description = $_REQUEST['description'];
       if($description=="全部"){
           $description='';
       }
       $this->assign('cid',$description);
       $list = Model('card')->tongji($description,$page);     
       $description = Model('card')->get_description();
       $this->assign('result',$list['data']);       
       $this->assign('description',$description);
       $this->assign('user_list_page',$list['pagelink']); 
       $this->display('tongji.html');
   }

   //导出活动列表
   function execlTJ()
   {
       $description = $_GET['description'];
       if($description=="全部"){
           $description=null;
       }
       $title = array("卡券名称","领取量","领取时间");                      
       $result =Model('card')->listAll($description); 
       /* var_dump($result);
       exit; */
       exportData("卡券领取表",$title,$result);        
       exit;
   }
   
   public function get_card_user()
   {
       $cid = intval($_GET['id']);
       $page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
       $list=Model('card')->get_user_list($cid,$page);
       $this->assign('sql_list',$list['data']);
       $this->assign('sql_page',$list['pagelink']);
       $this->display('get_card_user.html');
        
   }

   public function exportCardUser()
   {
       $title = array("姓名","电话","总数","领取量","领取时间");
       $cid = intval($_GET['id']);
       $name = $_REQUEST['name'];
       $result = Model('card')->getUserAll($cid);
       exportData($name."用户领取表",$title,$result);
       exit;
   }
}?>